﻿
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Expression.Interactivity;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using System;
using System.Windows.Shapes;

namespace Schedule0
{

    public class DragBehavior : Behavior<UIElement>
    {
        private bool isDragging = false;
        private UIElement attachedElement;
        private Window parent;

        Point lastPosition;
        Point originalPosition;
        TranslateTransform translatePosition;

        protected override void OnAttached()
        {
            attachedElement = this.AssociatedObject;
            parent = Application.Current.MainWindow;

            attachedElement.MouseLeftButtonDown += new MouseButtonEventHandler(MouseIsDown);
            attachedElement.MouseLeftButtonUp += new MouseButtonEventHandler(MouseIsUp);
            attachedElement.MouseMove += new MouseEventHandler(MouseIsMoving);

            
        }

        void MouseIsMoving(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Point currentPosition = e.GetPosition(parent);

                double dX = currentPosition.X - lastPosition.X;
                double dY = currentPosition.Y - lastPosition.Y;

                this.lastPosition = currentPosition;

                Transform oldTransform = attachedElement.RenderTransform;
                TransformGroup rt = new TransformGroup();
                TranslateTransform newPos = new TranslateTransform();
                newPos.X = dX;
                newPos.Y = dY;

                translatePosition = newPos;
                if (oldTransform != null)
                {
                    rt.Children.Add(oldTransform);
                }
                rt.Children.Add(newPos);

                MatrixTransform mt = new MatrixTransform();
                mt.Matrix = rt.Value;

                attachedElement.RenderTransform = mt;
            }
        }

        void MouseIsUp(object sender, MouseButtonEventArgs e)
        {
            isDragging = false;

            attachedElement.ReleaseMouseCapture();

            
            Point currentPosition = originalPosition;

            double dX = currentPosition.X - lastPosition.X;
            double dY = currentPosition.Y - lastPosition.Y;

            this.lastPosition = currentPosition;

            Transform oldTransform = attachedElement.RenderTransform;
            TransformGroup rt = new TransformGroup();
            TranslateTransform newPos = new TranslateTransform();
            newPos.X = dX;
            newPos.Y = dY;

            translatePosition = newPos;
            if (oldTransform != null)
            {
                rt.Children.Add(oldTransform);
            }
            rt.Children.Add(newPos);

            //MatrixTransform mt = new MatrixTransform();
            //mt.Matrix = rt.Value;

            //attachedElement.RenderTransform = mt;

            attachedElement.RenderTransform = rt;

            //double to = 100;
            //double duration = 1000;
            //DoubleAnimation anim = new DoubleAnimation(to, TimeSpan.FromMilliseconds(duration));
            //anim.AccelerationRatio = 0.2;
            //anim.DecelerationRatio = 0.7;
            //Storyboard aniSequence = new Storyboard();
            //aniSequence.Children.Add(anim);
            //Storyboard.SetTarget(anim, newPos);
            //Storyboard.SetTargetProperty(anim, new PropertyPath(TranslateTransform.YProperty));
            //aniSequence.Begin();

        }

        void MouseIsDown(object sender, MouseButtonEventArgs e)
        {
            isDragging = true;
            lastPosition = e.GetPosition(parent);
            originalPosition = lastPosition;
            attachedElement.CaptureMouse();
        }
    }
}
